package cn.player.act;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.player.R;
import cntv.player.core.util.LogUtil;

/**
 * Created by zengliang on 16/12/4.
 */
public class LeakSuspectActivity extends BaseActivity implements View.OnClickListener {


    private static final String TAG = "zl";
    private TextView mTvMessage;
    private Button mBtnLeak, mBtnLeakStop;
    private Thread mLeakThread;
    private List<String> mTestMsgList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak_suspect);

        findViews();
    }

    private void findViews() {
        mTvMessage = (TextView) findViewById(R.id.tv_message);
        mBtnLeak = (Button) findViewById(R.id.btn_leak_suspect);
        mBtnLeakStop = (Button) findViewById(R.id.btn_leak_suspect_stop);
        mBtnLeak.setOnClickListener(this);
        mBtnLeakStop.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLeakThread != null) {
            LogUtil.i(TAG, "mLeakThread.getName(): " + mLeakThread.getName());
            LogUtil.i(TAG, "mLeakThread.isAlive(): " + mLeakThread.isAlive());
            LogUtil.i(TAG, "log 4, mLeakThread.isInterrupted(): " + mLeakThread.isInterrupted());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_leak_suspect:
                if (mLeakThread == null) {
                    mLeakThread = new Thread(new LeakSuspectTask(),"Thread-LeakSuspect");
                }
                if (!mLeakThread.isAlive()) {
                    mLeakThread.start();
                }
                break;

            case R.id.btn_leak_suspect_stop:
                if (mLeakThread != null) {
                    LogUtil.i(TAG, "log 1,mLeakThread.isInterrupted(): " + mLeakThread.isInterrupted());
                    //TODO 中断线程，此时runnable遇等待任务将会抛InterruptedException异常
                    mLeakThread.interrupt();
                    //TODO 线程一旦被中断，mLeakThread.isInterrupted()便会返回true
                    LogUtil.i(TAG, "log 2,mLeakThread.isInterrupted(): " + mLeakThread.isInterrupted());
                }
                break;
        }
    }

    class LeakSuspectTask implements Runnable {

        @Override
        public void run() {
            int n = 1;
            while (true) {
                try {
                    Thread.sleep(1000);
                    final int fn = n;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(mTestMsgList == null){
                                mTestMsgList = new ArrayList<>();
                            }
                            String msg = "内存泄漏测试消息展示" + fn;
                            mTestMsgList.add(msg);
                            mTvMessage.setText(msg);
                            LogUtil.i(TAG, msg);
                        }
                    });
                    n++;
                    /*if(n == 10){
                        LogUtil.i(TAG, "log 11,Thread.interrupted(): " + Thread.interrupted());//输出false
                        LogUtil.i(TAG, "log 11,Thread.currentThread().getName(): " + Thread.currentThread().getName());
                        //TODO 中断当前线程,此时runnable并不会抛InterruptedException异常
                        Thread.currentThread().interrupt();
                        //TODO 当前线程被中断后，Thread.interrupted()将立即返回true,并自动重新置回false，
                        LogUtil.i(TAG, "log 12,Thread.interrupted(): " + Thread.interrupted());//输出true
                        LogUtil.i(TAG, "log 13,Thread.interrupted(): " + Thread.interrupted());//输出false
                        break;
                    }*/
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //TODO 抛异常后，中断标志被清空，mLeakThread.isInterrupted()将返回false
                    LogUtil.i(TAG, "log 3,mLeakThread.isInterrupted(): " + mLeakThread.isInterrupted());
                    break;
                }
            }
        }
    }
}
